{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "arr = random.randrange(10)\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from turtle import *\n",
    "from random import *\n",
    "from math import *\n",
    "\n",
    "\n",
    "def tree(n, l):\n",
    "    pd()  # 下笔\n",
    "    # 阴影效果\n",
    "    t = cos(radians(heading() + 45)) / 8 + 0.25\n",
    "    pencolor(t, t, t)\n",
    "    pensize(n / 3)\n",
    "    forward(l)  # 画树枝\n",
    "\n",
    "    if n > 0:\n",
    "        b = random() * 15 + 10  # 右分支偏转角度\n",
    "        c = random() * 15 + 10  # 左分支偏转角度\n",
    "        d = l * (random() * 0.25 + 0.7)  # 下一个分支的长度\n",
    "        # 右转一定角度，画右分支\n",
    "        right(b)\n",
    "        tree(n - 1, d)\n",
    "        # 左转一定角度，画左分支\n",
    "        left(b + c)\n",
    "        tree(n - 1, d)\n",
    "\n",
    "        # 转回来\n",
    "        right(c)\n",
    "    else:\n",
    "        # 画叶子\n",
    "        right(90)\n",
    "        n = cos(radians(heading() - 45)) / 4 + 0.5\n",
    "        pencolor(n, n * 0.8, n * 0.8)\n",
    "        circle(3)\n",
    "        left(90)\n",
    "\n",
    "        # 添加0.3倍的飘落叶子\n",
    "        if (random() > 0.7):\n",
    "            pu()\n",
    "            # 飘落\n",
    "            t = heading()\n",
    "            an = -40 + random() * 40\n",
    "            setheading(an)\n",
    "            dis = int(800 * random() * 0.5 + 400 * random() * 0.3 + 200 * random() * 0.2)\n",
    "            forward(dis)\n",
    "            setheading(t)\n",
    "\n",
    "            # 画叶子\n",
    "            pd()\n",
    "            right(90)\n",
    "            n = cos(radians(heading() - 45)) / 4 + 0.5\n",
    "            pencolor(n * 0.5 + 0.5, 0.4 + n * 0.4, 0.4 + n * 0.4)\n",
    "            circle(2)\n",
    "            left(90)\n",
    "            pu()\n",
    "\n",
    "            # 返回\n",
    "            t = heading()\n",
    "            setheading(an)\n",
    "            backward(dis)\n",
    "            setheading(t)\n",
    "\n",
    "            pu()\n",
    "            backward(l)  # 退回\n",
    "\n",
    "            bgcolor(0.5, 0.5, 0.5)  # 背景色\n",
    "            ht()  # 隐藏turtle\n",
    "            speed(0)  # 速度，1-10渐进，0最快\n",
    "            tracer(0, 0)\n",
    "            pu()  # 抬笔\n",
    "            backward(100)\n",
    "            left(90)  # 左转90度\n",
    "            pu()  # 抬笔\n",
    "            backward(300)  # 后退300\n",
    "            tree(12, 100)  # 递归7层\n",
    "            done()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
